//
//  YCEarningListViewController.m
//  lianganyuchu
//
//  Created by iMac on 17/2/23.
//  Copyright © 2017年 杭州桑缇电子商务有限公司. All rights reserved.
//

#import "YCEarningListViewController.h"

@interface YCEarningListViewController ()
{
    UITableView *m_tableView;
    NSInteger _selectIndex;
    NSMutableArray *m_arrayBtns;
    
    UIButton *m_btnMonth;
    YCMonthView *m_monthView;
    NSMutableArray *m_arrayList;
    int _currentPage;
    UIView *_footer;
}
@end

@implementation YCEarningListViewController
-(void)viewWillAppear:(BOOL)animated
{
    [_naviVC setNaviHidden:NO];
    [_naviVC setMainNaviVC];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    m_btnMonth=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 90, 36)];
    [m_btnMonth setTitle:[NSString stringWithFormat:@"%@月",_month] forState:UIControlStateNormal];
    [m_btnMonth setImage:[UIImage imageNamed:@"arrow_down"] forState:UIControlStateNormal];
    [m_btnMonth setTitleColor:WHITE_COLOR forState:UIControlStateNormal];
    [m_btnMonth.titleLabel setFont:[UIFont LightFontOfSize:18]];
    self.navigationItem.titleView=m_btnMonth;
    [m_btnMonth setImageEdgeInsets:UIEdgeInsetsMake(13, 74, 13, 0)];
    [m_btnMonth setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 10)];
    [m_btnMonth addTarget:self action:@selector(buttonMonthClicked:) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.leftBarButtonItem=[self tbarBackButtonWhite];
    
    _currentPage=1;
    m_arrayBtns=[NSMutableArray array];
    m_arrayList=[NSMutableArray array];
    [self addMainView];
    
    m_monthView=[[YCMonthView alloc]initWithFrame:CGRectMake(0, kNAV_HEIGHT, kMainScreen_Width, kMainPage_Height)];
    NSInteger index=_month.integerValue-1;
    [m_monthView addSubViewsWith:index];
    [self.view addSubview:m_monthView];
    m_monthView.monthBlock=^(NSString *title){
        [m_monthView setHidden:YES];
        [m_btnMonth setImage:[UIImage imageNamed:@"arrow_down"] forState:UIControlStateNormal];
        [m_btnMonth setTitle:title forState:UIControlStateNormal];
        _month=[title substringToIndex:title.length-1];
        _currentPage=1;
        [self loadData];
    };
    [m_monthView setHidden:YES];
    
    [self loadData];
}
-(void)loadData
{
    StatisticsMonthRequest *request = [StatisticsMonthRequest new];
    [apiClient hideProgress];
    [apiClient disableAfterRequest];
    request.page=[NSString stringWithFormat:@"%d",_currentPage];
    request.month=_month;
    request.deal_type=[NSString stringWithFormat:@"%ld",_selectIndex];
    [apiClient doStatisticsMonth:request success:^(ApiResponse *data, NSString *url) {
        StatisticsMonthResponse *response=[[StatisticsMonthResponse alloc]initWithCGResponse:data];
        if (_currentPage==1) {
             m_arrayList=[NSMutableArray arrayWithArray:response.items];
        }else{
            [m_arrayList addObjectsFromArray:response.items];
        }
        [m_tableView reloadData];
        m_emptyView.hidden=(m_arrayList.count>0);
        
        if (response.items.count==0) {
            m_tableView.mj_footer.state=MJRefreshStateNoMoreData;
        }else{
            [m_tableView.mj_footer endRefreshing];
        }
    }failure:^(ApiResponse *data, NSString *url){
    }];
}

-(void)addMainView
{
    UIView *backView=[[UIView alloc]init];
    [backView setBackgroundColor:MAIN_COLOR];
    [self.view addSubview:backView];
    [backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(0);
        make.right.equalTo(self.view).offset(0);
        make.top.equalTo(self.view).offset(kNAV_HEIGHT);
        make.height.mas_equalTo(50);
    }];
    
    m_tableView=[[UITableView alloc]init];
    m_tableView.separatorStyle=UITableViewCellSeparatorStyleSingleLine;
    m_tableView.delegate=self;
    m_tableView.dataSource=self;
    [self.view addSubview:m_tableView];
    [m_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(0);
        make.right.equalTo(self.view).offset(0);
        make.top.equalTo(backView.bottom).offset(0);
        make.bottom.equalTo(self.view).offset(0);
    }];
    
    m_tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _currentPage=1;
        [self loadData];
        [m_tableView.mj_header endRefreshing];
    }];
    m_tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        _currentPage++;
        [self loadData];
        [m_tableView.mj_header endRefreshing];
    }];
    
    _footer=[[UIView alloc]initWithFrame:CGRectMake(0, 0, kMainScreen_Width, 1.0)];
    UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(kDistance, 0, W(_footer)-2*kDistance, 1)];
    [label setBackgroundColor:LINE_COLOR];
    m_tableView.tableFooterView=_footer;
    [_footer addSubview:label];
    
    
    m_emptyView=[[YCEmptyView alloc]initWithFrame:CGRectMake(0, 0, kMainScreen_Width, H(m_tableView))];
    [m_emptyView setEmptyWith:@"public_nodata" Title:@"暂无数据"];
    [m_tableView addSubview:m_emptyView];
    [m_emptyView setHidden:YES];
    
    
    UIView *buttonView=[[UIView alloc]init];
    [backView addSubview:buttonView];
    [buttonView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(kDistance);
        make.right.equalTo(self.view).offset(-kDistance);
        make.top.equalTo(self.view).offset(kNAV_HEIGHT+kSmallDistance);
        make.height.mas_equalTo(30);
    }];
    buttonView.layer.borderColor=WHITE_COLOR.CGColor;
    buttonView.layer.borderWidth=1.0f;
    buttonView.layer.cornerRadius=2.0f;
    CGFloat width=(W(self.view)-2*kDistance)/3;
    
    
    _selectIndex=(_deal_type.integerValue>0) ? _deal_type.integerValue : 0;
    NSArray *titles=@[@"全部",@"收入",@"支出"];
    for (int i=0; i<3; i++) {
        UIButton *button=[[UIButton alloc]init];
        [button setFrame:CGRectMake(width*i, 0, width, 30)];
        [button setTitleColor:WHITE_COLOR forState:UIControlStateNormal];
        [button setTitleColor:MAIN_COLOR forState:UIControlStateSelected];
        [button setBackgroundImage:[UIImage imageWithColor:MAIN_COLOR] forState:UIControlStateNormal];
        [button.titleLabel setFont:[UIFont LightFontOfSize:15]];
        [button setBackgroundImage:[UIImage imageWithColor:WHITE_COLOR] forState:UIControlStateSelected];
        [button setTitle:titles[i] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
        button.tag=i;
        if (i==_selectIndex) {
            [button setSelected:YES];
        }
        [buttonView addSubview:button];
        [m_arrayBtns addObject:button];
    }
    
    [buttonView.layer addSublayer:getLine(width, width, 0, 30, WHITE_COLOR)];
    [buttonView.layer addSublayer:getLine(2*width, 2*width, 0, 30, WHITE_COLOR)];
}
-(void)buttonClicked:(UIButton *)button
{
    _selectIndex=button.tag;
    
    for (int i=0; i<m_arrayBtns.count; i++) {
        UIButton *button=m_arrayBtns[i];
        if (_selectIndex==i) {
            [button setSelected:YES];
        }else{
            [button setSelected:NO];
        }
    }
    _currentPage=1;
    [self loadData];
}
#pragma mark tableview datasource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return m_arrayList.count;
}
#pragma mark tableview delegate
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return CGFLOAT_MIN;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return CGFLOAT_MIN;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellIdentifier = [NSString stringWithFormat:@"cell%ld%ld",(long)indexPath.section,(long)indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (nil == cell){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
    }
    [cell.textLabel setFont:[UIFont LightFontOfSize:15]];
    [cell.detailTextLabel setFont:[UIFont LightFontOfSize:14]];
    [cell.textLabel setTextColor:TEXT_DEEP];
    [cell.detailTextLabel setTextColor:TEXT_MIDDLE];
    [cell.detailTextLabel setTextAlignment:NSTextAlignmentRight];
    NSDictionary *dict=m_arrayList[indexPath.row];
    [cell.textLabel setText:dict[@"text"]];
    [cell.detailTextLabel setText:dict[@"amount"]];
    
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (MODEL_VERSION >= 7.0) {
        if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
            [cell setSeparatorInset:UIEdgeInsetsMake(0, kDistance, 0, kDistance)];
        }
        if (MODEL_VERSION >= 8.0) {
            if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
                [cell setPreservesSuperviewLayoutMargins:NO];
            }
            if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
                [cell setLayoutMargins:UIEdgeInsetsMake(0, kDistance, 0, kDistance)];
            }
        }
    }
    
}

-(void)buttonMonthClicked:(UIButton *)button
{
    if (m_monthView.hidden) {
        [m_monthView setHidden:NO];
        [m_btnMonth setImage:[UIImage imageNamed:@"arrow_up"] forState:UIControlStateNormal];
    }else{
        [m_monthView setHidden:YES];
        [m_btnMonth setImage:[UIImage imageNamed:@"arrow_down"] forState:UIControlStateNormal];
    }

}

@end
